from configs.dbConfig import *
from redis import Redis

connection = Redis(
    host=REDIS_HOST,
    port=REDIS_PORT,
    password=REDIS_PASS,
    db=0,
    socket_timeout=10 * 60
)


def get_key(key):
    res = connection.get(key)
    if res:
        return str(res, encoding='utf-8')
    else:
        return False


def set_key(key, value, ex=-1):
    connection.set(key, value, ex=ex)


def del_key(key):
    connection.delete(key)


def push_element(key, value):
    connection.lpush(key, value)
    
def pop_element(key):
    value = connection.rpop(key)
    if value:
        return str(value, encoding='utf-8')


def popSet(key):
    value = connection.spop(key)
    if value:
        return str(value, encoding='utf-8')


def sadd_element(key, value):
    connection.sadd(key, value)


def getSet(key):
    liveIds = []
    for i in range(connection.scard(key)):
        try:
            liveIds.append(str(connection.spop(key), encoding='utf-8'))
        except Exception:
            continue
    return liveIds
